<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { GetBarChart } from '@/api/user'
const animationDuration = 2000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    }
  },
  data() {
    return {
      chart: null,
      status_c: [],
      status_f: [],
      status_t: [],
      status_w: [],
      status_n: []
    }
  },
  created() {
    // 获取userid
    this.getUserAdmin()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    // 获取管理员列表
    async getUserAdmin() {
      const { status_c, status_f, status_t, status_w, status_n } = await GetBarChart()
      this.status_c = status_c
      this.status_f = status_f
      this.status_t = status_t
      this.status_w = status_w
      this.status_n = status_n
      this.initChart()
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      const that = this
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        title: {
          text: '实习补贴申报数量', // 图表的标题
          left: 'center', // 标题居中
          top: 'top', // 标题位置在顶部
          textStyle: {
            color: '#333', // 字体颜色设置为黑色
            fontWeight: 'bold' // 加粗字体
          }
        },
        grid: {
          top: 80,
          left: '2%',
          right: '2%',
          bottom: '5%',
          containLabel: true
        },
        legend: {
          data: ['Forest', 'Steppe', 'Desert', 'Wetland']
        },
        xAxis: [{
          type: 'category',
          data: ['上级配套奖励', '实习交通补贴', '基地实习补贴', '基地建设奖励'],
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: '初审中',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: that.status_c,
          animationDuration
        }, {
          name: '复审中',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: that.status_f,
          animationDuration
        }, {
          name: '审核通过',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: that.status_t,
          animationDuration
        }, {
          name: '已完成',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: that.status_w,
          animationDuration
        }, {
          name: '未通过',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: that.status_n,
          animationDuration
        }]
      })
    }
  }
}
</script>
